package com.example.service;

import com.example.domain.User;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public List<User> find() {
        return userMapper.find();
    }

    public int add(String username, String password, String realname) {
        // 检查用户名是否存在
        User user = userMapper.selectByUsername(username);
        if (user != null) {
            return 0;
        }
        // 生成一个id
        String id = UUID.randomUUID().toString();
        return userMapper.add(id, username, password, realname);
    }

    public int update(User user) {
        return userMapper.update(user);
    }

    public int delete(String id) {
        return userMapper.delete(id);
    }
}
